package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.EvaluationStudentEntity;
import com.tfjybj.itoo.exam.model.EvaluationStudentModel;
import com.tfjybj.itoo.exam.model.QueryTeacherByStudentIdModel;
import com.tfjybj.itoo.exam.model.QuestionTypeModel;
import com.tfjybj.itoo.exam.provider.service.EvaluationStudentService;
import com.tfjybj.itoo.exam.provider.service.QuestionTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * EvaluationStudentController
 * evaluationStudent表
 *
 * @author 贾建清
 * @version 2.0.0
 * @since 2.0.0 2019-05-02 15:20:23
 */
@Api(tags = {"evaluationStudent表接口"})
@RequestMapping(value = "/evaluationStudent")
@RestController
public class EvaluationStudentController {

    @Resource
    private EvaluationStudentService evaluationStudentService;
    @Resource
    private QuestionTypeService questionTypeService;

    //region 模板生成：基本增删改

    /**
     * 查询出所有的题型
     * 王华伟---2019年5月4日11:48:04
     * @return
     */
    @ApiOperation(value = "查询出所有的题型")
    @GetMapping(value = {"/questionType"})
    public ItooResult selectQuestionType() {
        List<QuestionTypeModel> resultList = questionTypeService.queryAllQuestionType();
        if (CollectionUtils.isEmpty(resultList)){
            return ItooResult.build(ItooResult.SUCCESS,"没有查询出数据",null);
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", resultList);
    }

    @ApiOperation(value = "根据题型查询题干")
    @GetMapping(value = {"/selectQuestionContentByType/{id}"})
    public ItooResult selectQuestionContentByType() {
        List<QuestionTypeModel> resultList = questionTypeService.queryAllQuestionType();
        if (CollectionUtils.isEmpty(resultList)){
            return ItooResult.build(ItooResult.SUCCESS,"没有查询出数据",null);
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", resultList);
    }


    /**
     * 添加
     *
     * @param model EvaluationStudentModel
     * @return 添加的结果
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody EvaluationStudentModel model) {
		if (StringUtils.isEmpty(model.getTeacherId())){
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
		if (StringUtils.isEmpty(model.getCourseId())){
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        EvaluationStudentEntity evaluationStudentEntity = new EvaluationStudentEntity();
        BeanUtils.copyProperties(model, evaluationStudentEntity);
        evaluationStudentService.save(evaluationStudentEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        evaluationStudentService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        evaluationStudentService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model EvaluationStudentModel
     * @return 修改后的结果
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "根据id修改evaluationStudent")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody EvaluationStudentModel model) {
		if (StringUtils.isEmpty(model.getTeacherId())){
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
		if (StringUtils.isEmpty(model.getCourseId())){
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        EvaluationStudentEntity evaluationStudentEntity = new EvaluationStudentEntity();
        BeanUtils.copyProperties(model, evaluationStudentEntity);
        evaluationStudentService.updateById(evaluationStudentEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找EvaluationStudent
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        EvaluationStudentEntity evaluationStudentEntity = evaluationStudentService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", evaluationStudentEntity);
    }

    /**
     * 分页查询所有EvaluationStudent
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 贾建清
     * @since 2.0.0 2019-05-02 15:20:23
     */
    @ApiOperation(value = "分页查询所有EvaluationStudent")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<EvaluationStudentEntity> evaluationStudents = evaluationStudentService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", evaluationStudents);
    }
	
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据studentId查询需要被评价的老师
     * @param studentId
     * @author 刘竞博
     * @since 2.0.0 2019-05-02 15:20:23
     * @return
     */
    @ApiOperation(value = "根据studentId查询需要被评价的老师")
    @GetMapping(value = {"queryTeacher/{studentId}"})
   public ItooResult queryTeacher(@ApiParam(value = "studentId",required = true)@PathVariable String studentId){
        List<QueryTeacherByStudentIdModel> model = evaluationStudentService.queryTeacherByStudentId(studentId);
        return ItooResult.build("0000","查询成功",model);
   }

    /**
     * 添加模板--2019年5月26日09:38:47
     *
     * @param model EvaluationStudentModel
     * @return 添加的结果
     * @author 王华伟
     * @since 2.0.0 2019年5月26日09:38:47
     */
    @ApiOperation(value = "发布问卷")
    @PostMapping(value = {"/createTemplate"})
    public ItooResult createTemplate(@RequestBody EvaluationStudentModel model) {
        if (StringUtils.isEmpty(model.getTeacherId())){
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())){
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        //判断学生ID是否为空
        if(StringUtils.isEmpty(model.getStudentIds())){
            return  ItooResult.build(ItooResult.FAIL,"学生id不能为空");
        }
        String[] stuIDS = model.getStudentIds();
        EvaluationStudentEntity evaluationStudentEntity = new EvaluationStudentEntity();
        if(stuIDS.length>0){
            for (String stu:stuIDS){
                model.setStudentId(stu);
                BeanUtils.copyProperties(model, evaluationStudentEntity);
                evaluationStudentEntity.setStartTime(new Date());
                evaluationStudentService.save(evaluationStudentEntity);
            }
        }
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }


    /**
     * 根据模板Id更新endTime
     * @param id
     * @Param endTime
     * @author 白爱民
     * @since 2019年7月20日09:24:53
     * @return
     */
    @ApiOperation(value = "根据模板Id更新endTime")
    @GetMapping(value = {"updateEndTime/{id}"})
    public ItooResult updateEndTime(
            @ApiParam(value = "id",required = true)@PathVariable String id) {
        if ( StringUtils.isEmpty(id)){
            return ItooResult.build(ItooResult.FAIL,"更新失败",null);
        }else {
            Date endTime = new Date();
            Date startTime = new Date();
            boolean isUpdateEndTime = evaluationStudentService.updateEndTime(id,endTime);
            return ItooResult.build(ItooResult.SUCCESS,"更新成功",isUpdateEndTime);
        }
    }
}    
